package engine.display.guide
{
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.event.CustomEvent;
	import engine.guide.GuideEvent;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.newUi.EmbattleLogic;
	import engine.scene.SceneChangeManager;
	import engine.scene.SceneManager;
	import engine.scene.player.BasePlayerModel;
	import engine.scene.player.NpcModel;
	import engine.ui.MainUIManager;
	import engine.ui.UIManage;
	import engine.ui.lite.BaseUI;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Point;
	
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.loader.SwfLoader;

	public class GuideLogic
	{
		private static var _self:GuideLogic;
		/**数据模型**/
		private var _model:ModelLocator;
		/***当前需要显示的位置**/
		private var _curPoint:Point;
		/**显示界面***/
		private var _ui:GuideUI;
		private var _currGuideId:String;
		public var _currGuideObj:Object;
		private var guideArrow:MovieClip ;
		private var _currNpc:NpcModel;
		private var _currNpcId:String;
		public function GuideLogic()
		{
			
		}
		
		public static function getInstance():GuideLogic
		{
			if(_self == null)
			{
				_self =new GuideLogic();
			}
			return _self;
		}
		
		public function uiinit():void
		{
			removeAll();
			_model = ModelLocator.getInstance();
			_model.addEventListener(GuideEvent.EXECUTE_GUILD,onExecuteGuild);
			_model.addEventListener(GuideEvent.CLOSE_GUIDE,onCloseGuild);
			
			_model.addEventListener( String(CustomToClientFlag.SERVER_CUSTOMMSG_YLZG_ENTRY_BATTLESCENE), entryScene );
			
//			SceneManager.getInstance().norSceneView.sceneBG.addChild(guideArrow);
		}

		private function entryScene(e:CustomMsgEvent):void
		{
			ui.hide();	
		}
			
		
		/**
		 *接受触发引导 
		 * @param e
		 * 
		 */
		public function onExecuteGuild(e:GuideEvent):void
		{
//			showSwf();
			ui.show(StageInfo.guideSpace);
			_currGuideId = e.data[0];
			_currGuideObj = StaticIni.getIniObj(_currGuideId);
			if(_currGuideObj == null) return;
			getAllGuide();
		}
		
		
		/**
		 *关闭引导 
		 * @param e
		 * 
		 */
		public function onCloseGuild(e:GuideEvent):void
		{
			ui.hide();
		}
		
		/**
		 *获得显示位置 
		 * 
		 */
		public function getAllGuide():void
		{

			///需要显示位置的类型1：NPC对象 	2：界面对象  3：场景对象
			var guidetype:int = int(_currGuideObj.CoorTeam)
			
			switch (guidetype)
			{
				case 1:
					getNpcPoint();
					break;
				case 2:
					getConterPoint();
					break;
				case 3:
					getStagePoint();
					break;
				default :
					setShowType();
			}
		}
		
		/**
		 *根据舞台定位 
		 * 
		 */
		private function getStagePoint(obj:DisplayObject = null):void
		{
			var arr:Array = _currGuideObj.L.split(",")
			var fx:int = int(arr[0]);
			var width:int = int(arr[1]);
			///1 2 3 4 . 上,下,左,右
			var point:Point = new Point;
			
			var selfw:int = 0;
			var selfh:int = 0;
			if(obj)
			{
				selfw = obj.width;
				selfh = obj.height;
			}
			
			switch(fx)
			{
				case 1:
					point.x =  SceneChangeManager.getGameWidth()/2;
					point.y =  width ;
					break;
				case 2:
					point.x =  SceneChangeManager.getGameWidth()/2;
					point.y = SceneChangeManager.getGameHeight() - width  ;
					break;
				case 3:
					point.x =  width - selfw;
					point.y =  SceneChangeManager.getGameHeight()/2;
					break;
				case 4:
					point.x =  SceneChangeManager.getGameWidth() - width ;
					point.y =  SceneChangeManager.getGameHeight()/2;
					break;
			}
			_curPoint = point;
			setShowType();
		}
		
		private function removeAll():void
		{
			if(guideArrow)
			{
				guideArrow.visible = false;
			}
			if(ui.jiantou)
			{
				ui.jiantou.visible = false;
			}
			if(ui.zhixiangjiatou)
			{
				ui.zhixiangjiatou.visible = false;
			}
			ui.txt_info.visible = false;
			ui.npcFace.visible = false;
			
			ui.txt_test.visible = false
			ui.bg_test.visible = false
			ui.btn_test.visible = false
			ui.npcTalkBack.visible = false;
			ui.baseBG.visible = false;
//			if(ui.guideSwf)
//			{
//				ui.guideSwf.visible = false;
//			}
		}
		
		/**
		 * 设置显示内容
		 * 1：箭头
			2：NPC对话框
			3：文本框
			4：框
			5：图片
			6：动画
			7：指向箭头
		 * 
		 */
		private function setShowType():void
		{
			removeAll();
			try
			{
				var arr:Array = _currGuideObj.GuideTeam.split(",");
			} 
			catch(error:Error) 
			{
				return;
			}
			
			for(var i:int = 0;i<arr.length;i++)
			{
				switch (int(arr[i]))
				{
					case 1:
						showjiantou();
						break;
					case 2:
						showNPctalk();
						break;
					case 3:
						showTxt();
						break;
					case 4:
						
						break;
					case 5:
						
						break;
					case 6:
						showSwf();
						break;
					case 7:
						showZXjiantou();
						break;
					case 8:
						removeAll();
						break;
					
				}
			} 
		}
		
		
		public function showSwf():void
		{
//			ui.show(StageInfo.guideSpace);
			var url		:String = 	ResPath.SWF + _currGuideObj.FlashPhoto + ".swf" ;
			if(_currGuideObj.FlashPhoto == "GuideFlash_1")return;
//			var url		:String = 	ResPath.SWF + "123456" + ".swf" ;
			SwfLoader.StartLoad( url, null, onLoadRoleComplete, [_currGuideObj.FlashPhoto] );
			
		}
		
		/**
		 *成功加载资源 
		 * @param val
		 * @param pList
		 * 
		 */
		public function onLoadRoleComplete(val:DisplayObject,pList:Array):void
		{
			if(val == null)
			{
//				trace("加载资源"+pList[0]+"失败");
				return;
			}
			if(pList[0] == "GuideFlash_1")
			{
				return;
			}
			MainUIManager.getInstance().autoFoundWay	=	false;
//			trace("加载资源"+pList[0]+"成功");
			ui.guideSwf.source = val as MovieClip;
			var swf:MovieClip = val as MovieClip;
			var point : Point = ui.guideSwf.parent.globalToLocal( new Point( ( StageInfo.stage.stageWidth - ui.guideSwf.width ) * .5,
				( StageInfo.stage.stageHeight - ui.guideSwf.height ) * .5 ) );
			ui.guideSwf.x = point.x;
			ui.guideSwf.y = point.y;
			ui.guideSwf.visible = true;
			swf.gotoAndPlay(1);
			swf.addFrameScript(swf.totalFrames-1,mcstop);
			function mcstop():void
			{
				if(pList[0] == "GuideFlash_1")
				{
					EmbattleLogic.getInstance().guideHook();
				}
				swf.stop();
				ui.guideSwf.visible = false;
				swf.addFrameScript(swf.totalFrames-1,null);
			}
		}
		
		/**
		 *显示文本框 
		 * 
		 */
		private function showTxt():void
		{
			ui.txt_info.visible = true;
//			ui.txt_info.htmlStr = (StaticIni.getSwfLangStr(_currGuideObj.TalkString)==null)?"字符串没找到..为空":StaticIni.getSwfLangStr(_currGuideObj.TalkString);
		}
		
		
		/**
		 *显示箭头函数 
		 * 
		 */
		private function showjiantou():void
		{
			if(guideArrow!=null)
			{
				if(guideArrow.parent!=null)
				{
					guideArrow.parent.removeChild(guideArrow);
				}
			}
			
			guideArrow = null;
			guideArrow = ResManager.newResClass(_currGuideObj.ArrowName) as MovieClip;
			
			
			///需要显示位置的类型1：NPC对象 	2：界面对象  3：场景对象
			var guidetype:int = int(_currGuideObj.CoorTeam)
			
			switch (guidetype)
			{
				case 1:
					guideArrow.x = _curPoint.x ; //+ SceneChangeManager.getGameX();
					guideArrow.y = _curPoint.y -210;// + SceneChangeManager.getGameY()
					SceneManager.getInstance().norSceneView.guide.addChild(guideArrow);
					break;
				case 2:
					guideArrow.x = _curPoint.x ;
					guideArrow.y = _curPoint.y ;
					var addui:BaseUI = UIManage.getInstance().getUI(int(_currGuideObj.ObjTeam.split("_")[1]));
					if(addui.windowBG!=null)
					{
						addui.windowBG.addChild(guideArrow);
					}else
					{
						addui.addChild(guideArrow);
					}
					
					break;
				case 3:
					guideArrow.x = _curPoint.x + SceneChangeManager.getGameX();
					guideArrow.y = _curPoint.y + SceneChangeManager.getGameY();
					ui.addChild(guideArrow);
					break;
			}
			///1,2,3,4   ..上,下,左右.
			switch(int(_currGuideObj.ArrowWay))
			{
				case 1:
					guideArrow.rotation = 180;
					break;
				case 2:
					guideArrow.rotation = 90;
					break;
				case 3:
					guideArrow.rotation = 270;
					break;
				case 4:
					guideArrow.rotation = 360;
					break;
			}
		}
		
		/**
		 *显示NPC对话框 
		 * 
		 */
		private function showNPctalk():void
		{
			ui.npcFace.source = "res\\photos\\half_"+_currGuideObj.HalfPhoto+".png";
			ui.txt_info.htmlStr = StaticIni.getSwfLangStr(_currGuideObj.TalkString);
			ui.npcFace.visible = true;
			ui.txt_info.visible = true;
			ui.npcTalkBack.visible = true;
			ui.baseBG.visible = true;
		}
		
		/**
		 *显示指向性箭头 
		 * 
		 */
		private function showZXjiantou():void
		{
			if(ui.zhixiangjiatou.parent!=null)
			{
				ui.zhixiangjiatou.parent.removeChild(ui.zhixiangjiatou);
			}
			ui.zhixiangjiatou = null;
			ui.zhixiangjiatou =  ResManager.newResClass(_currGuideObj.ArrowName) as MovieClip;
			var showtxt:String = StaticIni.getSwfLangStr(_currGuideObj.TalkString);
			if(ui.zhixiangjiatou.numChildren>1)
			{
				ui.zhixiangjiatou.getChildAt(1)["text"].htmlText = showtxt==null?"":showtxt;//StaticIni.getSwfLangStr(_currGuideObj.TalkString);
			}
			///需要显示位置的类型1：NPC对象 	2：界面对象  3：场景对象
			var guidetype:int = int(_currGuideObj.CoorTeam)
			
			switch (guidetype)
			{
				case 1:
					ui.zhixiangjiatou.x = _curPoint.x +10 //+ SceneChangeManager.getGameX();
					ui.zhixiangjiatou.y = _curPoint.y -240// + SceneChangeManager.getGameY()
					SceneManager.getInstance().norSceneView.guide.addChild(ui.zhixiangjiatou);
					break;
				case 2:
					ui.zhixiangjiatou.x = _curPoint.x ;
					ui.zhixiangjiatou.y = _curPoint.y ;
					var addui:BaseUI = UIManage.getInstance().getUI(int(_currGuideObj.ObjTeam.split("_")[1]));
					if(addui.windowBG!=null)
					{
						addui.windowBG.addChild(ui.zhixiangjiatou);
					}else
					{
						addui.addChild(ui.zhixiangjiatou);
					}
					
					break;
				case 3:
					ui.zhixiangjiatou.x = _curPoint.x + SceneChangeManager.getGameX();
					ui.zhixiangjiatou.y = _curPoint.y + SceneChangeManager.getGameY();
					ui.addChild(ui.zhixiangjiatou);
					break;
			}
			ui.zhixiangjiatou.visible = true;
		}
		

		
		/**
		 *统一设置对象显示坐标 
		 * 
		 */
		private function setAllPoint(dis:DisplayObject):void
		{
			
			///需要显示位置的类型1：NPC对象 	2：界面对象  3：场景对象
			var guidetype:int = int(_currGuideObj.CoorTeam)
			
			switch (guidetype)
			{
				case 1:
					guideArrow.x = _curPoint.x +10 //+ SceneChangeManager.getGameX();
					guideArrow.y = _curPoint.y -240// + SceneChangeManager.getGameY()
					SceneManager.getInstance().norSceneView.guide.addChild(guideArrow);
					break;
				case 2:
					guideArrow.x = _curPoint.x + SceneChangeManager.getGameX();
					guideArrow.y = _curPoint.y + SceneChangeManager.getGameY();
					ui.addChild(guideArrow);
					break;
				case 3:
					
					break;
			}
		}
		
		
		/**
		 *根据NPC configid获取坐标 
		 * @param id
		 * 
		 */
		private function getNpcPoint():Point
		{
			_currNpcId = _currGuideObj.ObjTeam;
			var npcs : Array = SceneManager.getInstance().norSceneView.scenePlayerLogic.getNpcs();
			
			for(var i:int = 0;i<npcs.length;i++)
			{
				var npc:NpcModel = npcs[i] as NpcModel;
				var point:Point =  new Point(npc.x,npc.y);	
				if(npc.playerObj.GetProperties("ConfigID") == _currNpcId)
				{
					_currNpc = npc;
					_curPoint = point;
					setShowType();
					return point;
				}
			}
			//等待添加对应门点资源
			_model.addEventListener( CustomEvent.ADD_NPC, checkNpc );
			return null;
		}
		
		
		/**
		 * 添加NPC 
		 * @param e
		 * 
		 */		
		private function checkNpc(e:CustomEvent):void
		{
			var checkModel : BasePlayerModel	=	null;
			checkModel = e.data[ 0 ];
			
			if( checkModel.playerObj.GetPropertiesEx( "ConfigID") == _currNpcId )
			{//门点有了
				_currNpc = checkModel as NpcModel;
				_curPoint = _currNpc.getGuidePoint();
				setShowType();
				_model.removeEventListener( CustomEvent.ADD_NPC, checkNpc );
			}
		}
		
		/**
		 *根据界面坐标定位坐标
		 * @param uiname
		 * @param conName
		 * @return 
		 * 
		 */
		private function getConterPoint():Point
		{
			var curUI:BaseUI = 	UIManage.getInstance().getUI(int(_currGuideObj.ObjTeam.split("_")[1]));
			
			var pointArr:Array  = _currGuideObj.Coor.split(",");
			
//			var point:Point = curUI.localToGlobal( new Point(pointArr[0],pointArr[1]) )
			var point:Point =  new Point(pointArr[0],pointArr[1]) ;
			_curPoint = point;
			setShowType();
			return point;
		}
		
		/**
		 *隐藏所有的控件 
		 * 
		 */
		private function hideAllCon():void
		{
			
		}
		
		/**
		 *添加监听 
		 * 
		 */
		private function addListenter():void
		{

		}
		
		/**
		 *移除监听 
		 * 
		 */
		private  function removeListenter():void
		{
			
		}
		
		public function BeforeShow():void
		{
			addListenter();
		}
		
		public function AfterHide():void
		{
			removeListenter();
			removeAll();
		}
		
		public function get ui():GuideUI
		{
			if(_ui==null)
			{
				_ui	=	new GuideUI();
			}
			return _ui
		}
	}
}